package com.hxkj.service.impl;

import com.hxkj.dao.AuthRoleFunctionDao;
import com.hxkj.dao.FunctionDao;
import com.hxkj.domain.enums.FuncitonExceptionEnum;
import com.hxkj.domain.enums.FunctionSuccessEnum;
import com.hxkj.domain.po.AuthFunction;
import com.hxkj.domain.po.AuthRoleFunction;
import com.hxkj.domain.po.FunctionTree;
import com.hxkj.service.FunctionService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.result.ResultPageStruct;
import com.platform.common.domain.to.PageTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Mrmas
 * @create 2018-05-21 17:40
 * @desc 功能列表实现类
 **/

@Service
@Slf4j
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    private FunctionDao functionDao;

    @Autowired
    private AuthRoleFunctionDao authRoleFunctionDao;


    @Override
    public ResultPageStruct getFunctionList(AuthFunction authFunction, PageTo pageParam) throws Exception {
        ResultPageStruct resultPageStruct = new ResultPageStruct();
        try {
            List<FunctionTree> functionTrees = new ArrayList<>();
            FunctionTree functionTree = new FunctionTree();
            functionTree.setFunctionId(-1L);
            functionTree.setFunctionName("云平台管理");
            functionTree.setChildren(initTree(-1, authFunction));
            functionTrees.add(functionTree);
            resultPageStruct.setData(functionTrees);
            resultPageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
            resultPageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultPageStruct.setCode(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR.getCode());
            resultPageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR.getMsg());
        }
        return resultPageStruct;
    }

    private List<FunctionTree> initTree(long id, AuthFunction aufc) {
        List<FunctionTree> functionTrees = new ArrayList<>();
        AuthFunction authFunction = new AuthFunction();
        authFunction.setParentFunctionId(id);
        authFunction.setSiteId(aufc.getSiteId());
        authFunction.setFunctionName(aufc == null ? "" : aufc.getFunctionName().trim());
        functionDao.getFunctionList(authFunction).parallelStream().forEach(auf -> {
            Thread thread = Thread.currentThread();
            log.info("当前执行的线程为:{}",thread.getName());
            FunctionTree functionTree = new FunctionTree();
            functionTree.setFunctionName(auf.getFunctionName() == null ? "" : auf.getFunctionName());
            functionTree.setFunctionUri(auf.getFunctionUri() == null ? "" : auf.getFunctionUri());
            functionTree.setFunctionId(auf.getFunctionId());
            functionTree.setParentFunctionId(auf.getParentFunctionId());
            functionTree.setOrderId(auf.getOrderId());
            functionTree.setFunctionCode(auf.getFunctionCode() == null ? "" : auf.getFunctionCode());
            functionTree.setFunctionStatus(auf.getFunctionStatus() == null ? Byte.parseByte("") : auf.getFunctionStatus());
            functionTree.setFunctionType(auf.getFunctionType() == null ? Byte.valueOf("") : auf.getFunctionType());
            functionTree.setFunctionIcon(auf.getFunctionIcon() == null ? "" : auf.getFunctionIcon());
            functionTree.setFunctionLibraryId(auf.getFunctionLibraryId());
            // 避免跟搜索混淆
            auf.setFunctionName("");
            functionTree.setChildren(initTree(auf.getFunctionId(), auf));
            functionTrees.add(functionTree);
        });
        return functionTrees;
    }

    @Override
    public ResultMessageStruct saveAuthFunction(AuthFunction authFunction) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            int i = functionDao.saveAuthFunction(authFunction);

            resultMessageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
            resultMessageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_SAVE_ERROR.getCode());
            resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_SAVE_ERROR.getMsg());
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct updateAuthFunction(AuthFunction authFunction) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            if (authFunction.getFunctionStatus() != 0) {
                //禁用前判断 功能是否已经分配， 已经分配提示先解除该分配的功能
                AuthRoleFunction authRoleFunction = new AuthRoleFunction();
                authRoleFunction.setFunctionId(authFunction.getFunctionId());
                List<AuthRoleFunction> li = authRoleFunctionDao.get(authRoleFunction);
                if (li.size() > 0) {
                    resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_STATUS_ERROR.getCode());
                    resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_STATUS_ERROR.getMsg());
                    return resultMessageStruct;
                }

            }
            int i = functionDao.updateAuthFunction(authFunction);
            if (i == 1) {
                //修改子节点状态
                updateFunctionStatus(authFunction.getFunctionId(), authFunction.getFunctionStatus());
                //返回成功状态
                resultMessageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
                resultMessageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_UPDATE_ERROR.getCode());
            resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_UPDATE_ERROR.getMsg());
        }
        return resultMessageStruct;
    }

    public void updateFunctionStatus(Long functionId, Byte functionStatus) {
        AuthFunction ath = new AuthFunction();
        ath.setParentFunctionId(functionId);
        // 根据id来查询父节点是否有他
        List<AuthFunction> list = functionDao.getFunctionList(ath);
        if (null == list || list.size() < 1) {
            return;
        }
        //修改所有子节点
        ath = new AuthFunction();
        ath.setParentFunctionId(functionId);
        ath.setFunctionStatus(functionStatus);
        functionDao.modifyFunctionStatus(ath);
        //递归修改子节点
        for (AuthFunction authFunction : list) {
            if (authFunction.getFunctionType() == 1) {
                updateFunctionStatus(authFunction.getFunctionId(), functionStatus);
            }
        }
    }

    @Override
    public ResultDataStruct queryAuthFunction(AuthFunction authFunction) {
        ResultDataStruct resultDataStruct = new ResultDataStruct();
        try {
            AuthFunction function = functionDao.queryAuthFunction(authFunction);
            resultDataStruct.setData(function);
            resultDataStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
            resultDataStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
        } catch (Exception e) {
            e.printStackTrace();
            resultDataStruct.setCode(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR.getCode());
            resultDataStruct.setMessage(FuncitonExceptionEnum.FUNCTION_QUERY_ERROR.getMsg());
        }
        return resultDataStruct;
    }

    @Override
    public ResultMessageStruct deleteAuthFunction(AuthFunction authFunction) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        AuthFunction ath = new AuthFunction();
        // 说明有子节点,则不能删除该节点
        ath.setFunctionId(authFunction.getFunctionId());
        AuthFunction currentFunction = functionDao.queryAuthFunction(ath);
        if (null == currentFunction) {
            resultMessageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
            resultMessageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
            return resultMessageStruct;
        }
        //判断是否有子节点
        ath = new AuthFunction();
        ath.setParentFunctionId(authFunction.getFunctionId());
        // 根据id来查询父节点是否有他
        List<AuthFunction> list = functionDao.getFunctionList(ath);
        // 说明有子节点,则不能删除该节点
        if (list != null && list.size() != 0) {
            resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_DELETE_NODE_ERROR.getMsg());
            resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_DELETE_NODE_ERROR.getCode());
            return resultMessageStruct;
        }
        try {
            int i = functionDao.deleteAuthFunction(authFunction);
            if (i == 1) {
                resultMessageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
                resultMessageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_DELETE_ERROR.getCode());
            resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_DELETE_ERROR.getMsg());
        }
        return resultMessageStruct;
    }

    @Override
    public ResultMessageStruct modifyOrderId(Map<String, Object> map) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        AuthFunction ath = new AuthFunction();
        String[] id = map.get("ids").toString().split(",");
        ath.setSiteId(Long.parseLong(map.get("siteId").toString()));
        for (int i = 0; i < id.length; i++) {
            try {
                ath.setFunctionId(Long.parseLong(id[i]));
                ath.setOrderId(i);
                int j = functionDao.modifyOrderId(ath);
                if (j == 1) {
                    resultMessageStruct.setCode(FunctionSuccessEnum.FUNCTION_SUCCESS.getCode());
                    resultMessageStruct.setMessage(FunctionSuccessEnum.FUNCTION_SUCCESS.getMsg());
                }
            } catch (Exception e) {
                e.printStackTrace();
                resultMessageStruct.setCode(FuncitonExceptionEnum.FUNCTION_UPDATE_ERROR.getCode());
                resultMessageStruct.setMessage(FuncitonExceptionEnum.FUNCTION_UPDATE_ERROR.getMsg());
            }

        }

        return resultMessageStruct;
    }


}
